package org.edu.modules.stu.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.edu.common.dto.BaseCountDto;
import org.edu.common.dto.ExcelPo;
import org.edu.common.utils.PoiUtils;
import org.edu.modules.stu.base.dto.VeBaseStudentDto;
import org.edu.common.dto.AllDto;
import org.edu.modules.stu.base.dto.VeBaseStudentInfoDto;
import org.edu.modules.stu.base.dto.VeStudentVo;
import org.edu.modules.stu.base.entity.VeBaseStudent;
import org.edu.modules.stu.base.entity.VeStuFamily;
import org.edu.modules.stu.base.service.VeBaseStudentInfoService;
import org.edu.modules.stu.base.service.VeBaseStudentService;
import org.edu.common.api.vo.Result;
import org.edu.common.base.PageDto;
import org.edu.common.dto.StatusDto;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.query.QueryGenerator;
import org.edu.modules.stu.base.service.VeStuFamilyService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = {"学生信息表"})
@RestController
@RequestMapping("/veBaseStudent")
@Slf4j
public class VeBaseStudentController{

    @Autowired
    private VeBaseStudentService veBaseStudentService;

    @Autowired
    private VeBaseStudentInfoService infoService;

    @Autowired
    private VeStuFamilyService veStuFamilyService;


    @ApiOperation("分页查询学生信息表")
    @PostMapping("/page")
    public Result<?> page(@RequestBody PageDto<VeBaseStudent> page) {
        QueryWrapper<VeBaseStudent> queryWrapper =  page.buildQueryWrapper(VeBaseStudent.class);
        return Result.OK(veBaseStudentService.pageDto(page, queryWrapper));
    }

    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @PostMapping("/all")
    public Result<?> all(@RequestBody AllDto<VeBaseStudent> page) {
        QueryWrapper<VeBaseStudent> queryWrapper = page.buildQueryWrapper(VeBaseStudent.class);
        List<VeBaseStudent> list = veBaseStudentService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }

    @AutoLog(value = "统计困难生")
    @ApiOperation(value="统计困难生", notes="统计困难生")
    @GetMapping(value = "/countKns")
    public Result<List<BaseCountDto>> countKns(@RequestParam(name="year", required = false) String year) {
        return Result.OK( veBaseStudentService.countDto(year));
    }


    @ApiOperation("导出数据）")
    @PostMapping("/export")
    public Result<?> export(@RequestBody PageDto<VeBaseStudent> page, HttpServletResponse resp) {
        QueryWrapper<VeBaseStudent> queryWrapper =  page.buildQueryWrapper(VeBaseStudent.class);
        List<ExcelPo> list = new ArrayList<>();
        list.add(new ExcelPo("身份证号","idcard"));
        list.add(new ExcelPo("学号","code"));
        list.add(new ExcelPo("姓名","name"));
        list.add(new ExcelPo("性别","sex","sexStr"));
        list.add(new ExcelPo("民族","mzm","mzm"));
        list.add(new ExcelPo("所属年级","gradeId"));
        list.add(new ExcelPo("入学年月","rxDate"));
        list.add(new ExcelPo("招生类型","rxDate"));
        list.add(new ExcelPo("修读专业","specText"));
        list.add(new ExcelPo("学制","xz","xz"));
        list.add(new ExcelPo("班级","bjText"));
        list.add(new ExcelPo("所在省份","province"));
        list.add(new ExcelPo("所在市","city"));
        list.add(new ExcelPo("所在区","county"));
        list.add(new ExcelPo("详情地址","address"));
        list.add(new ExcelPo("报名号","bmh"));
        list.add(new ExcelPo("健康状况","jkzkm","jkzkm"));
        list.add(new ExcelPo("政治面貌","zzmmm","zzmmm"));
        list.add(new ExcelPo("户口类别","hklbm","hklbm"));
        list.add(new ExcelPo("是否低保","sfdb","shifou"));
        list.add(new ExcelPo("就读方式","jdfs","jdfs"));
        list.add(new ExcelPo("状态","xsdqztm","stuStatye"));



/*
        list.add(new ExcelPo("所属院系ID","falId"));

        list.add(new ExcelPo("生源地省id","sProvince"));
        list.add(new ExcelPo("生源地市id","sCity"));
        list.add(new ExcelPo("生源地区id","sCounty"));
        list.add(new ExcelPo("是否是困难生 0=否  1=是","sfkns"));
        list.add(new ExcelPo("准考证号","zkzh"));
        list.add(new ExcelPo("考生号","ksh"));
        list.add(new ExcelPo("更新状态（0：未更新; 1：已更新）","updateStatus"));
        list.add(new ExcelPo("学生ID","stuId"));
        list.add(new ExcelPo("曾用名","cym"));
        list.add(new ExcelPo("出生日期","csDate"));
        list.add(new ExcelPo("籍贯","jg"));
        list.add(new ExcelPo("健康状况码","jkzkm"));
        list.add(new ExcelPo("政治面貌码","zzmmm"));

        list.add(new ExcelPo("是否是流动","sfsldrk"));

        list.add(new ExcelPo("家庭联系电话","homePhone"));
        list.add(new ExcelPo("特长","tc"));
        list.add(new ExcelPo("学生联系电话","phone"));
        list.add(new ExcelPo("电子信箱","email"));
        list.add(new ExcelPo("照片","photo"));
        list.add(new ExcelPo("即时通讯号","qq"));
        list.add(new ExcelPo("是否低保0否1是","sfdb"));
        list.add(new ExcelPo("毕业学校","byxx"));
        list.add(new ExcelPo("报名方式","bmfsm"));
        list.add(new ExcelPo("入学成绩","rxcj"));
        list.add(new ExcelPo("家庭地址是否公开 1公开2不公开","jtdzStatus"));
        list.add(new ExcelPo("联系电话是否公开 1公开2不公开","xslxdhStatus"));*/
                PoiUtils.downloadFile(resp,veBaseStudentService.pageDto(page, queryWrapper).getRecords(),list);
        return Result.OK();
    }

    @AutoLog(value = "学生信息表-分页列表查询")
    @ApiOperation(value="学生信息表-分页列表查询", notes="学生信息表-分页列表查询")
    @PostMapping(value = "/list")
    public Result<?> queryPageList(VeBaseStudent entity,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<VeBaseStudent> queryWrapper = QueryGenerator.initQueryWrapper(entity, req.getParameterMap());
        PageDto<VeBaseStudent> page = new PageDto<VeBaseStudent>(pageNo, pageSize);
        IPage<VeBaseStudent> pageList = veBaseStudentService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "通过id查询")
    @ApiOperation(value="通过id查询", notes="通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        VeBaseStudentDto dto = veBaseStudentService.findDtoById(id);
        VeBaseStudentInfoDto infoDto = infoService.findDtoByStuId(id);
        VeStudentVo veStudentVo = new VeStudentVo();
        veStudentVo.setStu(dto);
        veStudentVo.setStuInfo(infoDto);
        if(dto==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veStudentVo);
    }

    @AutoLog(value = "添加")
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody VeStudentVo dto) {
        veBaseStudentService.insertOrUpdate(dto , true);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "编辑")
    @ApiOperation(value="编辑", notes="编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody VeStudentVo dto) {
        veBaseStudentService.insertOrUpdate(dto , false);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "批量删除")
    @ApiOperation(value="批量删除", notes="批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        veBaseStudentService.delete(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }


    @AutoLog(value = "更新状态")
    @ApiOperation(value="更新状态")
    @PostMapping(value = "/updateStatus")
    public Result<?> updateStatus(@RequestBody StatusDto statusDto) {
        UpdateWrapper<VeBaseStudent> updateWrapper = new UpdateWrapper<>();
        if(statusDto.getIds()!=null && statusDto.getIds().size()>0){
            updateWrapper.set("status",statusDto.getStatus());
            updateWrapper.in("id",statusDto.getIds());
            veBaseStudentService.update(updateWrapper);
            return Result.OK("批量更新成功!");
        }else{
            return Result.error("未选择!");
        }
    }



    /**
     * 查询所有
     */
    @ApiOperation("查询所有")
    @GetMapping("/findFamilys")
    public Result<?> findFamilys(String stuId) {
        LambdaQueryWrapper<VeStuFamily> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(VeStuFamily::getStuId,stuId);
        List<VeStuFamily> list = veStuFamilyService.listByWrapper(queryWrapper);
        return Result.OK(list);
    }




    @ApiOperation(value="查询通过ID")
    @GetMapping(value = "/findByIds")
    public Result<?> findByIds(@RequestParam(name="ids",required=true) String ids) {
        LambdaQueryWrapper<VeBaseStudent> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(VeBaseStudent::getId,Arrays.asList(ids.split(",")));
        return Result.OK(veBaseStudentService.listByWrapper(wrapper));
    }



}
